﻿using Autofac;
using Microsoft.Extensions.DependencyInjection;
using Net8.DependencyInjection.InterFace;
using Net8.DependencyInjection.Model;
using System.Reflection;

namespace Net8
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 单例
            {
                //Console.WriteLine("Hello, World!");
                //// 1、创建依赖注入
                //ServiceCollection services = new ServiceCollection();
                ////2、注册ProductService
                //services.AddSingleton<ProductService>();
                //services.AddSingleton<ProductRepository>();

                //// 3、取对象(构造 ServiceProvider)
                //var ServiceProvider = services.BuildServiceProvider();

                //// 4、取对象 
                //ProductService productService = ServiceProvider.GetRequiredService<ProductService>();
                //ProductService productService1 = ServiceProvider.GetRequiredService<ProductService>();
                //productService.GetProduct();
                //productService1.GetProduct();
            }
            #endregion

            #region 瞬时
            {
                //Console.WriteLine("Hello, World!");
                //// 1、创建依赖注入
                //ServiceCollection services = new ServiceCollection();
                ////2、注册ProductService
                //services.AddTransient<ProductService>();
                //services.AddTransient<ProductRepository>();

                //// 3、取对象(构造 ServiceProvider)
                //var ServiceProvider = services.BuildServiceProvider();

                //// 4、取对象 
                //ProductService productService = ServiceProvider.GetRequiredService<ProductService>();
                //ProductService productService1 = ServiceProvider.GetRequiredService<ProductService>();
                //productService.GetProduct();
                //productService1.GetProduct();
            }
            #endregion

            #region 作用域 Scope
            //{
            //    Console.WriteLine("Hello, World!");
            //    // 1、创建依赖注入
            //    ServiceCollection services = new ServiceCollection();
            //    //2、注册ProductService
            //    services.AddScoped<ProductService>();
            //    services.AddScoped<ProductRepository>();

            //    /// 取对象
            //    var ServiceProvider = services.BuildServiceProvider();
            //    // 4、取对象 
            //    using (IServiceScope serviceScope = ServiceProvider.CreateScope())
            //    {
            //        ProductService productService = serviceScope.ServiceProvider.GetRequiredService<ProductService>();
            //        ProductService productService1 = serviceScope.ServiceProvider.GetRequiredService<ProductService>();
            //        productService.GetProduct();
            //        productService1.GetProduct();
            //    }
            //    using (IServiceScope serviceScope = ServiceProvider.CreateScope())
            //    {
            //        ProductService productService = serviceScope.ServiceProvider.GetRequiredService<ProductService>();
            //        ProductService productService1 = serviceScope.ServiceProvider.GetRequiredService<ProductService>();
            //        productService.GetProduct();
            //        productService1.GetProduct();
            //    }
            //}
            #endregion

            #region 接口注册
            {
                //Console.WriteLine("Hello, World!");
                //// 1、创建依赖注入
                //ServiceCollection services = new ServiceCollection();
                ////2、注册ProductService
                //services.AddScoped<IProductService, ProductService>();
                //services.AddScoped<IProductRepository, ProductRepository>();  // 接口注册 接口取 类的初始化传参为接口
                //services.AddScoped<IProductRepository, ProductRepositoryCS>();  // 接口注册 多实现类

                ///// 取对象
                //var ServiceProvider = services.BuildServiceProvider();
                //// 4、取对象 
                ////using (IServiceScope serviceScope = ServiceProvider.CreateScope())
                ////{
                ////    IProductService productService = serviceScope.ServiceProvider.GetRequiredService<IProductService>();
                ////    IProductService productService1 = serviceScope.ServiceProvider.GetRequiredService<IProductService>();
                ////    productService.GetProduct();
                ////    productService1.GetProduct();
                ////}
                //// 多实现类
                //using (IServiceScope serviceScope = ServiceProvider.CreateScope())
                //{
                //    IEnumerable<IProductRepository> productServices = serviceScope.ServiceProvider.GetServices<IProductRepository>();
                //    foreach (var productService in productServices)
                //    {
                //        productService.GetProductRepository();
                //    }
                //}
            }
            #endregion

            #region atuofac 属性注册 添加了PropertiesAutowired
            {
                //// 1、创建 ContainerBuilder
                //ContainerBuilder container = new ContainerBuilder();

                //container.RegisterType<ProductService>().As<IProductService>().PropertiesAutowired();
                //container.RegisterType<ProductRepository>().As<IProductRepository>().PropertiesAutowired();
                //container.RegisterType<ProductRepositoryCS>().As<IProductRepository>().PropertiesAutowired();

                ////2、 构造容器
                //var containerBuild = container.Build();
                //// 4、取对象 
                //using (var scope = containerBuild.BeginLifetimeScope())
                //{
                //    IProductRepository productRepository = scope.Resolve<IProductRepository>();
                //    productRepository.GetProductRepository();

                //    IProductService productService = scope.Resolve<IProductService>();
                //    productService.GetProduct();
                //}
            }
            #endregion

            #region 6\ atuofac 批量注册
            {
                //// 1、创建 ContainerBuilder
                //ContainerBuilder container = new ContainerBuilder();

                //container.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())  // Assembly.GetExecutingAssembly() 加载整个程序集
                //    .AsImplementedInterfaces()   // 只要有接口都实现注册
                //    .PropertiesAutowired();    // 属性注册

                ////2、 构造容器
                //var containerBuild = container.Build();
                //// 4、取对象 
                //using (var scope = containerBuild.BeginLifetimeScope())
                //{
                //    IProductRepository productRepository = scope.Resolve<IProductRepository>();
                //    productRepository.GetProductRepository();

                //    IProductService productService = scope.Resolve<IProductService>();
                //    productService.GetProduct();

                //    IOrderServer orderServer = scope.Resolve<IOrderServer>();
                //    orderServer.GetOrder();
                    
                //}
            }
            #endregion

            #region 7\ atuofac 批量注册 过滤(接口实现)
            {
                // 1、创建 ContainerBuilder
                //ContainerBuilder container = new ContainerBuilder();

                //container.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())  // Assembly.GetExecutingAssembly() 加载整个程序集
                //    .Where(T => !T.GetInterfaces().Contains(typeof(IInject)))  // 进行过滤 
                //    .AsImplementedInterfaces()   // 只要有接口都实现注册
                //    .PropertiesAutowired();    // 属性注册

                ////2、 构造容器
                //var containerBuild = container.Build();
                //// 4、取对象 
                //using (var scope = containerBuild.BeginLifetimeScope())
                //{
                //    IProductRepository productRepository = scope.Resolve<IProductRepository>();
                //    productRepository.GetProductRepository();

                //    IProductService productService = scope.Resolve<IProductService>();
                //    productService.GetProduct();

                //    IOrderServer orderServer = scope.Resolve<IOrderServer>();
                //    orderServer.GetOrder();

                //}
            }
            #endregion


            #region 8\ atuofac 批量注册 过滤(接口实现)
            {
                // 1、创建 ContainerBuilder
                ContainerBuilder container = new ContainerBuilder();

                container.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())  // Assembly.GetExecutingAssembly() 加载整个程序集
                    .Where(T => T.GetCustomAttributes<MyAttribute>().Any())  // 进行过滤 
                    .AsImplementedInterfaces()   // 只要有接口都实现注册
                    .PropertiesAutowired();    // 属性注册

                //2、 构造容器
                var containerBuild = container.Build();
                // 4、取对象 
                using (var scope = containerBuild.BeginLifetimeScope())
                {
                    IProductRepository productRepository = scope.Resolve<IProductRepository>();
                    productRepository.GetProductRepository();

                    IProductService productService = scope.Resolve<IProductService>();
                    productService.GetProduct();

                    IOrderServer orderServer = scope.Resolve<IOrderServer>();
                    orderServer.GetOrder();

                }
            }
            #endregion
        }
    }
}
